<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');
// 
//  pic_model.php
//  phpweb
//  
//  Created by WillLee on 2011-07-26.
//  Copyright 2011 lishuzu. All rights reserved.
// 


class Pic_model extends CI_Model
{
		function __construct(){
			parent::__construct();
			$this->load->model('user_model');
			$this->user = $this->session->userdata('user') ? $this->session->userdata('user') : '';
		}
		
		//表单是name pic
		//workid 所属作品 是作品图片
		//belongwid 所属作品 是对作品的晒图功能
		// thumb 是否生成缩略 1生成 0不生成
		function uploadpics($workid ='', $belongwid ='', $thumb = '1'){
			if (!$this->user) {
				return false;
			}
			
			if (empty($workid) && empty($belongwid)) {
				return false;
			}
			
			// 不能同时是作品图片和晒图图片
			if (!empty($workid) && !empty($belongwid)) {
				return false;
			}
			
			foreach ($_FILES as $filename => $value) {
				if (strpos($filename, 'ic_')) {
					if (!empty($value['name'])) {
						$this->uploadpic($filename, $workid, $belongwid);
					}
				}
			}
			return true;
		}
		
		function uploadpic($filename, $workid ='', $belongwid ='', $thumb = '1'){
			if (!$filename) {
				return false;
			}
			if (!$this->user) {
				return false;
			}
			
			if (empty($workid) && empty($belongwid)) {
				return false;
			}
			
			// 不能同时是作品图片和晒图图片
			if (!empty($workid) && !empty($belongwid)) {
				return false;
			}
			$setarr = array(
				'workid' => $workid,
				'uid' => $this->user['uid'],
				'name' => $this->user['name'],
				'dateline' => now(),
				'postip' => getonlineip(),
				'title' => '',
				'imgtype' => '',
				'size' => '',
				'filepath' => '',
				'thumb' => '0',	
				'belongwid' => $belongwid,
				'type' => '1',
				);
			$this->db->insert('pic', $setarr);
			$picid = $this->db->insert_id();
			
			$config = array();
			$config['file_name'] = 'pic_'.$picid.'_'.now().random(4);
			$config['max_width'] = 0;
			$config['max_height'] = 0;
			$config['max_size'] = '2000';
			$config['formname'] = $filename;
			$result = upload_file($config);
			if (isset($result['error'])) {
				return $result;
			}else{
				$updatedata = array(
						'size' => $_FILES[$filename]['size'],
						'imgtype' => $result['upload_data']['file_type'],
						'filepath' => $result['upload_data']['basename'].'/'.$result['upload_data']['file_name'],
					);
				$this->db->where('picid', $picid);
				$result = $this->db->update('pic', $updatedata);
			}
			
			if ($updatedata) {
				$filepath = $updatedata['filepath'];
				$bigfile =  FCPATH.'upload/attachments/'.get_workimg($filepath, 'big', 'path');
				$thumbfile =  FCPATH.'upload/attachments/'.get_workimg($filepath, 'thumb', 'path');
				
				// 压缩图片
				$this->load->library('image_lib');
				$config['image_library'] = 'gd2';
				$config['source_image'] = FCPATH.'upload/attachments/'.$filepath;
				$config['maintain_ratio'] = TRUE;
				
				//大图 width 600
				$config['new_image'] =$bigfile;
				$config['width'] = 600;
				$config['height'] = 600;
				$this->image_lib->initialize($config);
				$res = $this->image_lib->resize();
				
				// 水印
				$water = array();
				$water['source_image'] = $bigfile;
				$water['wm_text'] = "@".$this->user['name']." ".base_url().'home/index/'.$this->user['uid'];
				$water['wm_type'] = 'text';
				$water['wm_font_path'] = './system/fonts/jjz.ttf';
				$water['wm_font_size'] = '8';
				$water['wm_font_color'] = '515151';
				$water['wm_vrt_alignment'] = 'bottom';
				$water['wm_hor_alignment'] = 'right';
				$water['wm_hor_offset'] = '0';
				$water['wm_vrt_offset'] = '0';
				$this->image_lib->initialize($water);
				$this->image_lib->watermark();
				
				// 小图 180*180
				$config['new_image'] = $thumbfile;
				$config['width'] = 180;
				$config['height'] = 180;
				$this->image_lib->initialize($config);
				$res = $this->image_lib->resize();
				$this->db->where('picid', $picid);
				$result = $this->db->update('pic', array('thumb'=>'1'));
			}
			return true;
		}
		
		// 彻底删除 连同附件一起删除
		function delete($picids){
			if (!is_array($picids)) {
				$picids = array($picids);
			}
			
			if ($picids) {
				$this->db->where_in('picid', $picids);
				$query = $this->db->get('pic');
				foreach ($query->result_array() as $row) {
					$this->db->where('picid', $row['picid']);
					$this->db->delete('pic');
					
					// 删除图片
					$originimg = FCPATH.'upload/attachments/'.get_workimg($row['filepath'], 'origin', 'path');
					$bigimg = FCPATH.'upload/attachments/'.get_workimg($row['filepath'], 'big', 'path');
					$thumbimg = FCPATH.'upload/attachments/'.get_workimg($row['filepath'], 'thumb', 'path');
					@unlink($originimg);
					@unlink($bigimg);
					@unlink($thumbimg);
				}
				return true;
			}
			return false;
		}
}
?>